package com.soda.util;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport{
	private PageInfo pi;

	public PageInfo getPi() {
		return pi;
	}

	public void setPi(PageInfo pi) {
		this.pi = pi;
	}
	
	@Override
	public int doStartTag() throws JspException {
		StringBuffer txt=new StringBuffer();
		if(pi.getLastPage()<1){
			txt.append("<br/><div style='font-size:25px;text-align: center'>暂无查询结果</div>");
		}else{
		txt.append("<form action='"+pi.getUrl()+"' method='post'>");
		
		txt.append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
		txt.append("<tr>");
		txt.append("<td width='30%' height='25'>");
		txt.append("<table border='0' cellspacing='0' cellpadding='3'>");
		txt.append("<tr>");
		txt.append("<td style='width:45px'></td>");
	
		if(pi.getPage()==1){
			txt.append("<td style='width:60px'><span style='color:#666666'>首页</span></td>");
			txt.append("<td style='width:60px'><span style='color:#666666'>上一页</span></td>");

		}else{
			
			txt.append("<td style='width:60px'><a href='"+pi.getUrl()+"?page=1&param="+pi.getStr()+"'><span style='color:mediumspringgreen'>首页</span></a></td>");
			txt.append("<td style='width:60px'><a href='"+pi.getUrl()+"?page="+(pi.getPage()-1)+"&param="+pi.getStr()+"'><span style='color:mediumspringgreen'>上一页</span></a></td>");
		}
		
		for (int i = 1; i <= pi.getLastPage(); i++) {
			if(pi.getPage()==i){
				txt.append("<td style='width:30px'><span style='color:#666666'>"+i+"</span></td>");
				continue;
			}
			if(Math.abs(pi.getPage()-i)<=2){
				txt.append("<td style='width:30px'><a href='"+pi.getUrl()+"?page="+i+"&param="+pi.getStr()+"'><span style='color:mediumspringgreen'>"+i+"</span></a></td>");
				continue;
			}
		}
		if(pi.getPage()==pi.getLastPage()){
			txt.append("<td style='width:60px'><span style='color:#666666'>下一页</span></td>");
			txt.append("<td style='width:60px'><span style='color:#666666'>尾页</span></td>");


		}else{
			
			txt.append("<td style='width:60px'><a href='"+pi.getUrl()+"?page="+(pi.getPage()+1)+"&param="+pi.getStr()+"'><span style='color:mediumspringgreen'>下一页</span></a></td>");
			txt.append("<td style='width:60px'><a href='"+pi.getUrl()+"?page="+pi.getLastPage()+"&param="+pi.getStr()+"'><span style='color:mediumspringgreen'>尾页</span></a></td>");
		}
		txt.append("</tr></table></td>");
		
		txt.append("<td width='20%' align='center'>");
		txt.append("<table border='0' cellspacing='0' cellpadding='3'>");
		txt.append("<tr>");
		
		txt.append("<td><a href='javascript:goto()'><img src='houtai/images/next.gif' width='16' height='16' border='0'></a></td>");
		txt.append("<td><input name='goto' id='goto' type='text' size='3' style='width:25;height:18'>/页</td>");
		
		//页面跳转
		txt.append("<script>function goto(){var v=document.getElementById('goto').value;var num=/^\\d{1,}$/;" +
				"if(num.test(v)){if(v>"+pi.getLastPage()+"||v<1){alert('不存在此页面');return;}else{location.href='"+pi.getUrl()+"?page='+v+'&param="+pi.getStr()+"';return;}}else{alert('请输入数字');return;}}</script>");
		txt.append("</tr></table></td>");		
		
		txt.append("<td width='20%' align='right'>共"+pi.getTotal()+"条记录显示到"+pi.getPage()+"/"+pi.getLastPage()+"</td>");
		txt.append("</tr></table>");
			
		txt.append("</form>");
		}
		PageContext pc = this.pageContext;

		JspWriter out = pc.getOut();
		try {
			out.append(txt.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return this.EVAL_PAGE;
	}

}
